﻿using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace TimeAlert
{
    public class ExamDto
    {
        public string Id { get; set; }
        public string ExamName { get; set; }

        [JsonConverter(typeof(CustomDateTimeConverter))]
        public DateTime StartTime { get; set; }
        [JsonConverter(typeof(CustomDateTimeConverter))]
        public DateTime EndTime { get; set; }
        public List<StageDto> StageList { get; set; }

    }

    public class StageDto
    {
        public string Name { get; set; }
        [JsonConverter(typeof(CustomDateTimeConverter))]
        public DateTime StartTime { get; set; }
        public string Audio { get; set; }
    }
}

public class CustomDateTimeConverter : JsonConverter
{
    private const string DateFormat = "yyyy-MM-dd HH:mm:ss";

    public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
    {
        if (value is DateTime dateTime)
        {
            writer.WriteValue(dateTime.ToString(DateFormat));
        }
    }

    public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
    {
        if (DateTime.TryParse(reader.Value.ToString(), out DateTime result))
        {
            return result;
        }
        return default(DateTime);
    }

    public override bool CanConvert(Type objectType)
    {
        return objectType == typeof(DateTime);
    }
}
